//
//  ExRecordViewController.m
//  IntegralDog
//
//  Created by HanenDev on 16/3/17.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "ExRecordViewController.h"
#import "Macro.h"
#import "ExchangeTableViewCell.h"
#import "ExchangeModel.h"

@interface ExRecordViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * _excTableView;
}

@property (nonatomic, strong) NSMutableArray  *dataArray;

@end

@implementation ExRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"兑换记录";
    self.navigationController.navigationBarHidden = NO;
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 22)];
    [btn addTarget:self action:@selector(backToView:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"Btn_Nourmal_Jiantou"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"Btn_Push_Jiantou"] forState:UIControlStateSelected];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = barItem;
    
    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    
    [self initData];
    
    _excTableView=[[UITableView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    //_excTableView=[UITableView new];
    _excTableView.delegate=self;
    _excTableView.dataSource=self;
    _excTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_excTableView];

}

- (void)initData{
    
    NSNumber *pageNum = [NSNumber numberWithInt:1];
    NSNumber *rowsNum = [NSNumber numberWithInt:20];
    NSDictionary *dic =@{@"token":TOKEN,@"uid":@(UID),@"page":pageNum,@"rows":rowsNum};
    
    [DataTool postWithUrl:EXCHANGE_RECORD_URL parameters:dic success:^(id data) {
        NSDictionary *jsonDic = IntegrationJsonParserWithData(data);
        int code = [jsonDic[@"code"]intValue];
        if (code != 100) {
            [self.view makeToast:jsonDic[@"message"]];
        }
        NSMutableArray *array = jsonDic[@"list"];
        for (int i=0; i<array.count; i++) {
            ExchangeModel *model = [[ExchangeModel alloc]init];
            [model setValuesForKeysWithDictionary:array[i]];
            [_dataArray addObject:model];
        }
        [_excTableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}
#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//
//    return 1;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 125;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"ExchangeTableViewCell";
    ExchangeTableViewCell *cell = (ExchangeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ExchangeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ExchangeModel *model = _dataArray[indexPath.row];
    NSString *imgStr = [NSString stringWithFormat:@"%@%@",IMAGEURL,model.litimg];
    [cell.excImageView sd_setImageWithURL:[NSURL URLWithString:imgStr]];
    cell.excNameLab.text = model.title;
    cell.excCodeLab.text = [NSString stringWithFormat:@"券码:%@",model.exchangeCode];
    cell.addressLab.text = [NSString stringWithFormat:@"兑换地址:%@",model.exchangeUrl];;
    cell.excValidityLab.text = [NSString stringWithFormat:@"有效期:%@",model.indate];
    cell.excIntegralLab.text = [NSString stringWithFormat:@"%d",model.needIntegral];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)backToView:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
